<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Console\Commands;

use App\Enums\CheckStatusEnum;
use App\Models\UserProfile;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CensusAreaUserCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'census:area_user_count';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计片区人数信息';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $result = UserProfile::query()->groupBy('area')
            ->select('area', DB::raw('count(*) as number'))
            ->where('check_status', CheckStatusEnum::CHECK_ADOPT)->get()->toArray();
        Redis::set(config('census.user_area_key'), json_encode($result));
        return 0;
    }
}
